/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 *
 * @author TranDangThuan
 */
public class DBUtility {
    public static Connection openConnection() {
        Connection con = null;
        try {
            FileInputStream fis = new FileInputStream("config.properties");
            Properties props = new Properties();
            props.load(fis);
            fis.close();
            
            Class.forName(props.getProperty("driver"));
            String url = "jdbc:sqlserver://" + props.getProperty("server") + ":";
            url += props.getProperty("port") + ";databaseName=" + props.getProperty("db");
            con = DriverManager.getConnection(url, props.getProperty("user"), props.getProperty("pass"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return con;
    }
    
    public static void closeResultSet(ResultSet rs) {
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
    public static void closeStatement(PreparedStatement stm) {
        if(stm != null){
            try {
                stm.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
    public static void closeConnection(Connection con) {
        if(con != null){
            try {
                con.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
}
